'use strict'

//此插件基于jQuery
function Pagination(_option){
    let defaultOption = {
        target: '#yq_pagination',
        callback: null,
        length: 5,
        sum: 5,
        showGroup: true
    }
    _option = $.extend({}, defaultOption, _option);
    this.target = $(_option.target);
    this.callback = _option.callback;
    this.length = _option.length;//每组显示的页数
    this.item = 0;//当前页
    this.group = 0;//当前组
    this.sum = _option.sum;//总页数
    this.groupSum = Math.ceil(_option.sum / _option.length);
    this.showGroup = _option.showGroup;

    this.init();
}

Pagination.prototype.init = function(){
    //当前组的页码
    let items = '';
    for(let i=0; i<this.length; i++){
        let displayItem = i + 1;
        let classNames = 'pag-item';
        i === this.item && (classNames += ' active');
        items += `<li class="${classNames}" value="${i}">${displayItem}</li>`;
    }
    //总页数
    let sum = this.sum;
    //当前页码
    let current = this.item + 1;
    //上一组、下一组
    let prevGroup = this.showGroup ? '<li class="pag-prev-group">&lt;&lt;</li>' : '',
        nextGroup = this.showGroup ? '<li class="pag-next-group">&gt;&gt;</li>' : '';

    let pagElement = `<ul class="yq-pagination">
            ${prevGroup}
            <li class="pag-prev">&lt;</li>
            <li class="pag-next">&gt;</li>
            ${nextGroup}
            <li class="pag-direct">
                <span>共<span class="direct-sum">${sum}</span>页</span>
                <span>第<input class="direct-current" value="${current}"/>页</span>
                <button class="direct-sure">确定</button>
            </li>
        </ul>`;
    this.target.html(pagElement);
    this.setGroup(this.group);
    this.setEvents();
}

Pagination.prototype.setGroup = function(_group, _item){
    debugger;
    this.group = +_group;
    let start = _group * this.length;
    let items = '';
    for(let i=start; i<start+this.length&&i<this.sum; i++){
        let displayItem = i + 1;
        let classNames = 'pag-item';
        items += `<li class="${classNames}" value="${i}">${displayItem}</li>`;
    }
    this.target.find('.pag-item').remove();
    this.target.find('.pag-prev').after(items);
    this.setItem(_item ? _item : start);
}

Pagination.prototype.setItem = function(_item){
    debugger;
    this.item = +_item;
    let current = +_item + 1;
    this.target.find('.direct-current').val(current);
    this.target.find('.pag-item').removeClass('active')
    this.target.find(`.pag-item[value=${_item}]`).addClass('active');
    this.callback && this.callback(_item);
}

Pagination.prototype.setEvents = function(_item){
    let self = this;
    //上一组
    this.target.find('.pag-prev-group').off().on('click', function(ele){
        if(self.group > 0){
            let group = self.group - 1;
            self.setGroup(group);
        }
    })
    //下一组
    this.target.find('.pag-next-group').off().on('click', function(ele){
        if(self.group + 1 < self.groupSum){
            let group = self.group + 1;
            self.setGroup(group);
        }
    })
    //上一页
    this.target.find('.pag-prev').off().on('click', function(ele){
        if(self.item > 0){
            let item = self.item - 1;
            let group = Math.floor(item / self.length);
            if(group < self.group){
                self.setGroup(group, item);
            }else{
                self.setItem(item);
            }
        }
    })
    //下一页
    this.target.find('.pag-next').off().on('click', function(ele){
        if(self.item + 1 < self.sum){
            let item = self.item + 1;
            let group = Math.floor(item / self.length);
            if(group > self.group){
                self.setGroup(group, item);
            }else{
                self.setItem(item);
            }
        }
    })
    //页码-指定页
    this.target.find('.yq-pagination').off('click').on('click', '.pag-item', function(ele){
        debugger;
        let item = ele.target.value;
        self.setItem(item);
    })
    //输入框-指定页
    this.target.find('.direct-sure').off().on('click', function(ele){
        debugger;
        let item = +self.target.find('.direct-current').val() - 1;
        if(item < 0){
            self.target.find('.direct-current').val(self.item + 1);
        }else if(item + 1 > self.sum){
            self.target.find('.direct-current').val(self.item + 1);
        }else{
            let group = Math.floor(item / self.length);
            if(group !== self.group){
                self.setGroup(group, item);
            }else{
                self.setItem(item);
            }
        }
    })
}

export default Pagination;